//
//  FileManagementViewController.m
//  Againstpoor
//
//  Created by 辰领科技 on 16/11/6.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "FileManagementViewController.h"
#import "FileManagementTableViewCell.h"
#import "FileManagementTwoViewController.h"

static NSString * Identifer = @"identifier";

@interface FileManagementViewController ()
<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate> {
    BOOL isSelected;
    NSInteger count;
    NSString *code;
}

@property (weak, nonatomic) IBOutlet UIView *navView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UIButton *search;
@property (weak, nonatomic) IBOutlet UIButton *BGbutton;
@property (weak, nonatomic) IBOutlet UIButton *searchButton;
@property (strong, nonatomic) NSMutableArray *TownArray;
@property (strong, nonatomic) NSMutableArray *CountryArray;
@end

@implementation FileManagementViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    isSelected = YES;
    _textField.alpha = 0.0f;
    _search.alpha = 0.0f;
    _BGbutton.alpha = 0.0f;
    count = 1;
    [self getpoorAreaTown];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _TownArray = [NSMutableArray array];
    _CountryArray = [NSMutableArray array];
    [self initViewLayout];
}
- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    
    _textField.layer.masksToBounds = YES;
    _textField.layer.cornerRadius = 12.5*VerticalRatio();
    _textField.leftView = [[UIView alloc]initWithFrame:flexibleFrame(CGRectMake(0, 0, 10, 1), NO)];
    _textField.leftViewMode = UITextFieldViewModeAlways;
    [_textField setValue:kGetColor(156, 156, 156) forKeyPath:@"_placeholderLabel.textColor"];
    
    _textField.delegate = self;
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomePageStoryboard" bundle:nil];
    FileManagementTwoViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"FileManagementTwo"];
    page.name = textField.text;
    [[Common getNavigation]pushViewController:page];
    [textField resignFirstResponder];
    return YES;
}

- (IBAction)back:(id)sender {
    if (count == 1) {
        [self backBtn];
    } else if (count == 2) {
        count--;
        [self getpoorAreaTown];
    }
    
}
#pragma mark - 镇列表
- (void)getpoorAreaTown {
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    self.shareApi.delegate = self;
    [self.shareApi  requestOperationWithDic:dic withPath:F_poorAreaTown withHttpType:@"POST" withTag:F_F_poorAreaTown];
}
#pragma mark - 村列表
- (void)getpoorAreaCountry {
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setObject:code forKey:@"code"];
    self.shareApi.delegate = self;
    [self.shareApi  requestOperationWithDic:dic withPath:F_poorAreaCountry withHttpType:@"POST" withTag:F_F_poorAreaCountry];
}
#pragma mark - 搜索
- (IBAction)search:(id)sender {
    isSelected = !isSelected;
    if (isSelected) {
        [UIView animateWithDuration:0.5 animations:^{
            _textField.alpha = 0.0f;
            _search.alpha = 0.0f;
            _BGbutton.alpha = 0.0f;
        }];
        [_searchButton setImage:[UIImage imageNamed:@"fangdajing1.png"] forState:UIControlStateNormal];
        [_searchButton setTitle:@"" forState:UIControlStateNormal];
    } else {
        [UIView animateWithDuration:0.5 animations:^{
            _textField.alpha = 1.0f;
            _search.alpha = 1.0f;
            _BGbutton.alpha = 1.0f;
        }];
        [_searchButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        [_searchButton setTitle:@"取消" forState:UIControlStateNormal];
        _textField.text = @"";
    }
}
- (IBAction)hidden:(id)sender {
    [UIView animateWithDuration:0.5 animations:^{
        _textField.alpha = 0.0f;
        _search.alpha = 0.0f;
        _BGbutton.alpha = 0.0f;
    }];
    [_searchButton setImage:[UIImage imageNamed:@"fangdajing1.png"] forState:UIControlStateNormal];
    [_searchButton setTitle:@"" forState:UIControlStateNormal];
}

#pragma mark - <UITableViewDelegate&&UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

//定义 UITableView cell 的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (count == 1) {
        return _TownArray.count;
    } else if (count == 2) {
        return _CountryArray.count;
    } else {
        return 0;
    }
    
}

//定义 UITableView cell 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45*VerticalRatio();
}

//定义 UITableView cell 展示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FileManagementTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:Identifer];
    if (cell == nil) {
        cell= [[FileManagementTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifer];
        
    }
    for (UIView *view in cell.subviews) {
        if ([view isKindOfClass:[UILabel class]] ||[view isKindOfClass:[UIImageView class]]||[view isKindOfClass:[UIScrollView class]]||[view isKindOfClass:[UIButton class]]) {
            [view removeFromSuperview];
        }
    }
    
    if (count == 1) {
        NSDictionary *dic = _TownArray[indexPath.row];
        cell.titleLabel.text = [Common stringFromData:dic[@"address"]];
        
    } else if (count == 2) {
        NSDictionary *dic = _CountryArray[indexPath.row];
        cell.titleLabel.text = [Common stringFromData:dic[@"address"]];
    }
    return cell;
}

//定义 UITableView cell 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (count == 1) {
        count++;
        code = [Common stringFromData:_TownArray[indexPath.row][@"code"]];
        [self getpoorAreaCountry];
    } else if (count == 2) {
        UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomePageStoryboard" bundle:nil];
        FileManagementTwoViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"FileManagementTwo"];
        page.area = [Common stringFromData:_CountryArray[indexPath.row][@"code"]];;
        [[Common getNavigation]pushViewController:page];
    }
    
}

#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{

    if (tag==F_F_poorAreaTown) {
        
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            _TownArray = database[@"data"];
            _tableView.dataSource = self;
            _tableView.delegate = self;
            [_tableView reloadData];
        }else  {
            
        }
    } else if (tag==F_F_poorAreaCountry) {
        
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            _CountryArray = database[@"data"];
            [_tableView reloadData];
        }else  {
            
        }
    }
}
-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
    [self dismissWaitDialog];
    
    
}

@end
